package com.cly.house.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cly.house.dao.SortDao;
import com.cly.house.dao.impl.SortDaoImpl;
import com.cly.house.entity.Sort;
import com.cly.house.uitl.PageInfo;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class SortServlet
 */
@WebServlet("/SortController")
public class SortController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       SortDao sortdao = new SortDaoImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SortController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String type = request.getParameter("type");
		if(type!=null) {
			switch (type) {
			case "list":
				list(request,response);
				break;
			case "toAdd":
				toAdd(request,response);
				break;
			case "add":
				add(request,response);
				break;
			case "toUpdate":
				toUpdate(request,response);
				break;
			case "update":
				update(request,response);
				break;
			case "delete":
				delete(request,response);
				break;
			case "listAll":
				listAll(request,response);
				break;
			default:
				break;
			}
		}
	}
	private void listAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		List<Sort> list = sortdao.listAll();
		String jsonStr = new ObjectMapper().writeValueAsString(list);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(jsonStr);
		
	}

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		/*List<Sort> sortList = sortdao.listAll();
		request.setAttribute("sortList", sortList);*/
		String current  = request.getParameter("current");
		int pageNum = 1;
		if(current!=null)
			pageNum = Integer.parseInt(current);
		PageInfo<Sort> pageInfo = sortdao.findByPage(pageNum, 3);
		String jsonStr = new ObjectMapper().writeValueAsString(pageInfo);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jsonStr);
	}
	protected void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.getRequestDispatcher("WEB-INF/jsp/sort/sort_add.jsp").forward(request, response);
	}
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String sname = request.getParameter("sort.sname");
		Sort sort = new Sort();
		sort.setSname(sname);
		int count = sortdao.save(sort);
		String jsonStr = new ObjectMapper().writeValueAsString(count);
		response.getWriter().write(jsonStr);
	}
	protected void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int sid = Integer.parseInt(request.getParameter("sid")) ;
		Sort sort = sortdao.updateId(sid);
		response.setCharacterEncoding("utf-8");
		String jsonStr = new ObjectMapper().writeValueAsString(sort);
		response.getWriter().write(jsonStr);
		
		
	}
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int sid =Integer.parseInt(request.getParameter("sort.sid"));
		String sname = request.getParameter("sort.sname");
		Sort sort = new Sort();
		sort.setSid(sid);
		sort.setSname(sname);
		int update = sortdao.update(sort);
		String jsonStr = new ObjectMapper().writeValueAsString(update);
		response.getWriter().write(jsonStr);
		
		
	}
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int sid =Integer.parseInt(request.getParameter("sid"));
		boolean delete = sortdao.delete(sid);
		String jsonStr = new ObjectMapper().writeValueAsString(delete);
		response.getWriter().write(jsonStr);
	}

}
